package com.techwells.learningkites.fragment;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import com.lidroid.xutils.BitmapUtils;
import com.techwells.learningkites.R;
import com.techwells.learningkites.adapter.CourseListAdapter;
import com.techwells.learningkites.adapter.MyListAdapter;
import com.techwells.learningkites.adapter.MySentenceListAdapter;
import com.techwells.learningkites.cache.UserCenter;
import com.techwells.learningkites.controller.LoginActivity;
import com.techwells.learningkites.controller.MainPageActivity;
import com.techwells.learningkites.controller.MyFavoriteAcitivity;
import com.techwells.learningkites.controller.MySententceActivity;
import com.techwells.learningkites.controller.MyWordActivity;
import com.techwells.learningkites.model.MainPageViewModel;
import com.techwells.learningkites.networkaccess.KitesRequestUrl;
import com.techwells.learningkites.networkservice.KitesServiceMediator;
import com.techwells.learningkites.networkservice.model.CourseCategory;
import com.techwells.learningkites.networkservice.model.UserProfile;
import com.techwells.taco.mvvm.Route;
import com.techwells.taco.net.HttpConnectionUtils;
import com.techwells.taco.net.HttpHandler;
import com.techwells.taco.tasktool.TaskToken;

import android.os.Bundle;
import android.os.Handler;
import android.provider.ContactsContract.CommonDataKinds.Nickname;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MyFragment extends Fragment implements OnItemClickListener,
		OnClickListener {

	private com.techwells.taco.widget.RoundImageView userIcon;
	private TextView nickName;

	private ListView myListView;
	private List<String> myList;
	private MyListAdapter myListAdapter;
	private String[] itemStr = { "我的课程", "我的视频", "我的收藏", "我的生词本", "我的例句" };

	private MainPageActivity activity;
	private BitmapUtils bitmapUtils;

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_my, container, false);
	}

	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		activity = (MainPageActivity) getActivity();
		bitmapUtils = new BitmapUtils(getActivity());
		initViews();
	}

	private void initViews() {
		userIcon = (com.techwells.taco.widget.RoundImageView) getActivity()
				.findViewById(R.id.user_icon);
		userIcon.setOnClickListener(this);
		nickName = (TextView) getActivity().findViewById(R.id.name);

		myListView = (ListView) getActivity().findViewById(R.id.lv_my);
		myList = new ArrayList<String>();
		for (int i = 0; i < 5; i++) {
			myList.add(itemStr[i]);
		}
		myListAdapter = new MyListAdapter(getActivity(), myList);
		myListView.setAdapter(myListAdapter);
		myListView.setOnItemClickListener(this);

	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view,
			int position, long id) {
		switch (position) {
		case 2:
			Route.route().nextController(getActivity(),
					MyFavoriteAcitivity.class.getName(),
					Route.WITHOUT_RESULTCODE);
			break;
		case 3:
			Route.route().nextController(getActivity(),
					MyWordActivity.class.getName(), Route.WITHOUT_RESULTCODE);
			break;
		case 4:
			Route.route().nextController(getActivity(),
					MySententceActivity.class.getName(),
					Route.WITHOUT_RESULTCODE);
			break;

		default:
			break;
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	public void refreshData(TaskToken token) {
		String method = token.method;
		if (method.equals(KitesServiceMediator.SERVICE_GET_PROFILE)) {
			UserProfile userProfile = ((MainPageViewModel) activity.baseViewModel).userProfile;
			UserCenter.getInstance().setUser(userProfile.user);
			MainPageActivity.isRefresh++;
			if (userProfile.user.nickName != null) {
				nickName.setText(userProfile.user.nickName);
			}else if (userProfile.user.avatar != null) {
				int index = userProfile.user.avatar.indexOf(".");
				if (index != -1) {
					String avatarUrlCheck = userProfile.user.avatar
							.substring(index + 1);
					if (avatarUrlCheck.equals("gif")) {
						bitmapUtils.display(userIcon,
								KitesRequestUrl.BASE_URL_IMG
										+ userProfile.user.avatar);
					}
				} else {
					userIcon.setBackgroundColor(getResources().getColor(
							R.color.white_1));
				}
			}else if (userProfile.user.avatar == null) {
				userIcon.setBackgroundColor(getResources().getColor(R.color.white_1));
			}

		}

	}

}
